546C - Soldier and Cards - CodeForces Solution


brute force dfs and similar games *1400

Please click on ads to support us..

Python Code:

n = int(input())

d1 = [int(x) for x in input().split()][1:]
d2 = [int(x) for x in input().split()][1:]

n_fights = 0
tolerance = 1000000

for i in range(tolerance):
	c1 = d1.pop(0)
	c2 = d2.pop(0)
	if c1 > c2:
		d1 += [c2, c1]
	else:
		d2 += [c1, c2]
	n_fights += 1
	if not d1 or not d2:
		break

if not d1:
	print(n_fights, 2)
elif not d2:
	print(n_fights, 1)
else:
	print(-1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
// queue<int>st1,st2;
queue<int> insertToBottom(queue<int>s,int data){
	queue<int>temp;
	while(!s.empty()){
		temp.push(s.front());
		s.pop();
	}
	s.push(data);
	while(!temp.empty()){
		s.push(temp.front());
		temp.pop();
	}
	return s;
}
int32_t main(){
	int n;	cin >> n;
	int k1;	cin >> k1;
	queue<int>st1;
	for(int i=0;i<k1;i++){
		int x;	cin >> x;
		st1.push(x);
	}
	int k2;	cin >> k2;
	queue<int>st2;
	for(int i=0;i<k2;i++){
		int x;	cin >> x;
		st2.push(x);
	}
	int cunt = 0;
	while(1){
		if(st1.size()==0 || st2.size()==0) break;
	 	int x = st1.front();
	 	st1.pop();
	 	int y = st2.front();
	 	st2.pop();
	 	if(x>y){
	 		// st1 = insertToBottom(st1,x);
	 		// st1 = insertToBottom(st1,y);
			st1.push(y);
			st1.push(x);
	 		cunt++;
	 	}
	 	else if(x<y){
	 		// st2 = insertToBottom(st2,y);
	 		// st2 = insertToBottom(st2,x);
			st2.push(x);
			st2.push(y);
	 		cunt++;
	 	}
		if(cunt>=n*n*n){
			cunt = -1;
			break;
		}
	 }
	//  cout << st1.size() << " " << st2.size() << endl;
	//  while(!st2.empty()){
	//  	cout << st2.front() << " ";
	//  	st2.pop();
	//  }
	 if(cunt!=0) cout << cunt << " ";
	//  else cout << -1 << " ";
	 if(st1.size()!=0 && cunt!=-1) cout << 1 << endl;
	 else if(cunt!=-1) cout << 2 << endl;
}
		  		  		 	    	    	   			  	


Comments

Submit
0 Comments
More Questions

814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III